package cn.edu.tju.ldj.cashup.webfetcher;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import cn.edu.tju.ldj.cashup.constant.Constant;

public class CCBFetcherImpl implements ICCBFetcher {

	private DefaultHttpClient httpclient = null;

	@Override
	public String getWebPage(String url) {
		httpclient = null;
		httpclient = new DefaultHttpClient();

		HttpGet httpget = new HttpGet(url);
		ResponseHandler<String> responseHandler = new BasicResponseHandler();
		String responseBody = "";
		try {
			responseBody = httpclient.execute(httpget, responseHandler);
		} catch (Exception e) {
//			e.printStackTrace();
			responseBody = null;
		} finally {
			httpget.abort();
		}
		return responseBody;
	}

	@Override
	public Map<String, Double> getAllData() {
		String priceInfoStr = this.getWebPage(Constant.URL);
		priceInfoStr = priceInfoStr.split("\\(\\[\\{")[1].split("\\}\\]\\)")[0].replaceAll("\"", "");
//		System.out.println(priceInfoStr);
		Map<String, Double> dataMap = new HashMap<String, Double>();
		if(priceInfoStr != null && !"".equals(priceInfoStr)){
			String [] subStr = priceInfoStr.split("\\},\\{");
			for (int i = 0; i < subStr.length; i++) {//这里将数据格式化hashmap
				String [] subsubStr = subStr[i].split(",");
				Map<String, String> newItem = new HashMap<String, String>();
				for (int j = 0; j < subsubStr.length; j++) {
					newItem.put(subsubStr[j].split(":")[0], subsubStr[j].split(":")[1]);
				}
				if(newItem.get("Code").contains(Constant.goldCode)){
					double price = 0;
					try {
						price = Double.parseDouble(newItem.get("Newest"));
//						System.out.println(price);
					} catch (Exception e) {
						// TODO: handle exception
						e.printStackTrace();
						price = 0;
					}
					dataMap.put(Constant.goldCode, price);
				}
				else if(newItem.get("Code").contains(Constant.silverCode)){
					double price = 0;
					try {
						price = Double.parseDouble(newItem.get("Newest"));
//						System.out.println(price);
					} catch (Exception e) {
						// TODO: handle exception
						e.printStackTrace();
						price = 0;
					}
					dataMap.put(Constant.silverCode, price);
				}
			}
		}
		return dataMap;
	}
	
	@Override
	public List<Map<String, Object>> getDayAvgData(String url){
		List<Map<String, Object>> resultList = new ArrayList<Map<String,Object>>();
		String dayAvgContent = this.getWebPage(url);
		if(dayAvgContent == null || "".equals(dayAvgContent)){
			return resultList;
		}
		String[] dayAvgItem = dayAvgContent.split("}");
		for (int i = 0; i < dayAvgItem.length; i++) {
			if(dayAvgItem[i].length() < 80){
				break;
			}
			dayAvgItem[i] = dayAvgItem[i].split("\\{")[1];
			Map<String, Object> item = new HashMap<String, Object>();
			String[] dataAttr = dayAvgItem[i].split("\",\"");
			item.put("time", dataAttr[0].split("\":\"")[1]);
			String[] dataAttrs = dataAttr[1].split(",\"");
			for (int j = 0; j < dataAttrs.length-1; j++) {
				item.put(dataAttrs[j].split("\":")[0], Double.valueOf(dataAttrs[j].split("\":")[1]));
			}
			resultList.add(item);
		}
		return resultList;
	}
	
	@Override
	public Double getNewPrice(String url){
		Double newPrice = null;
		String realTiemPriceContent = this.getWebPage(url);
		newPrice = Double.valueOf(realTiemPriceContent.split("\"new_pri\":\"")[1].split("\"")[0]);
		return newPrice;
	}

	public static void main(String[] args) {
		ICCBFetcher f = new CCBFetcherImpl();
		System.out.println(f.getAllData());
		f.getDayAvgData("http://tool.ccb.com/waihui/trendchart/day/019995.js");
		System.out.println(f.getNewPrice(Constant.goldenNewPriceUrl));
	}

}
